package com.gzbd.login.controller;

import com.gzbd.login.common.Result;
import com.gzbd.login.jwt.CustomTokenManager;
import com.gzbd.login.jwt.JwtTokenUtil;
import com.gzbd.login.jwt.JwtUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author xlj
 * @version 1.0
 * @description: TODO
 * @date 2024/2/22 15:04
 */
@RestController
@RequestMapping("/shiro")
public class ShiroController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${custom.jwt.expire_time}")
    private long expireTime;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private CustomTokenManager customTokenManager;

    @RequestMapping("/getToken")
    public Result getToken(){
        String token = jwtTokenUtil.generateToken("USER", "23a79685-d667-48c6-b863-f696419b456f", "522201", "04026909a06e4ffca9942edaada8e2e4");
//        String token = JwtUtil.sign("xsy", "123");
//        redisTemplate.opsForValue().set(token,token, expireTime*2/100, TimeUnit.SECONDS);
        customTokenManager.addToken(token,"23a79685-d667-48c6-b863-f696419b456f");
        return Result.success(token);
    }

    @RequestMapping("/deleteToken")
    public Result deleteToken(String token){
        customTokenManager.removeToken(token);
        return Result.success(token);
    }

//    @RequiresPermissions("user:admin")
    @RequestMapping("/test")
    public Result test(){
        System.out.println("进入测试，只有带有令牌才可以进入该方法");
        return Result.success(1,"访问接口成功");
    }
}
